package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.crm.domain.pojo.CrmRole;
import com.yu.crm.domain.pojo.Gate;
import com.yu.crm.domain.pojo.Shift;
import com.yu.crm.domain.pojo.Station;
import com.yu.crm.domain.response.GateResponse;
import com.yu.crm.mapper.GateMapper;
import com.yu.crm.service.IGateService;
import com.yxzz.common.exception.GeneralException;
import com.yxzz.common.helper.DataHelpers;
import com.yxzz.common.helper.valid.ValidatorHelpers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zds
 * @since 2021-03-12
 */
@Service
public class GateService extends ServiceImpl<GateMapper, Gate> implements IGateService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public void deleteByIds(String ids) {
        try {
            String substring = ids.substring(0, ids.length());
            String[] result = substring.split(",");
            for (String s : result) {
                Gate gate = baseMapper.selectById(Long.valueOf(s));
                if (DataHelpers.isNotEmpty(gate)) {
                    gate.setIsDeleted(1);
                    ValidatorHelpers.validateDataSave(baseMapper.updateById(gate));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            throw new GeneralException("操作失败");
        }
    }

    public Gate infoById(Long id){
        try {
            return baseMapper.selectById(id);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            throw new GeneralException("操作失败");
        }
    }

    public IPage getPage(Page<Gate> page, String condition){
        QueryWrapper<Gate> queryWrapper = new QueryWrapper<>();
        if(DataHelpers.isNotEmptyString(condition)){
            queryWrapper.and(i->i.like("station_name",condition)
            );
        }
        return baseMapper.selectPage(page,queryWrapper);
    }


    @Override
    public IPage<GateResponse> getGateResList(IPage<GateResponse> page, String stationName, String condition) { ;
        return baseMapper.getGateResList(page, stationName, condition);
    }
}
